﻿using System;
using System.Windows.Forms;
using MMonitor.BusinessLayer;
using MMonitor.PM6750;

namespace MMonitor.UI
{
    public partial class MainFormV2 : Form
    {
        private ReceivedDataModelV2 _dRModel = new ReceivedDataModelV2();

        BaseControlV2 bCtrV2 = new BaseControlV2();
        public MainFormV2()
        {
            InitializeComponent();
        }

        private bool isFirstData = false;
        /// <summary>
        /// 接收从bll返回的数据并通过Invoke更新
        /// </summary>
        /// <param name="rdmodel"></param>
        void UpdateReceivedData(ReceivedDataModelV2 rdmodel)
        {
            _dRModel = rdmodel;
            BeginInvoke(new EventHandler(DisplayReceivedData), rdmodel);//--此处使用并发方式更新界面
            Invoke(new EventHandler(DisplayReceivedData));
        }
        void DisplayReceivedData(object sender,EventArgs e)
        {
            if (!isFirstData)
            {
                label_STime.Text = DateTime.Now.ToString("G");
                isFirstData = true;
            }
            //配置显示元素（输入drReceiced加点之后有提示）
            //--显示基本数据
            //--根据数值绘制图形
            zyGraphicWave1.DrawPoint(_dRModel.ECGWaveData1.LeadData1);
            zyGraphicWave2.DrawPoint(_dRModel.ECGWaveData1.LeadData2);
            //textBox_ECG1.Text = _dRModel.ECGWaveData1.LeadData1.ToString();//ECG数据
            //textBox_E.Text = _dRModel.ECGWaveData1.LeadData2.ToString();//ECG数据
            //textBox_ECG3.Text = _dRModel.ECGWaveData1.LeadData3.ToString();//ECG数据
            //textBox_ECGavr.Text = _dRModel.ECGWaveData1.LeadData4.ToString();//ECG数据
            //textBox_RESP.Text = _dRModel.RESPWaveData.ToString();//呼吸波形数据
            //textBox_SPO2.Text = _dRModel.SATWaveData.ToString();//脉搏氧容积波形数据

            textBox_心率值.Text = _dRModel.ECGParamsData1.HeartRate.ToString();
            textBox_ST值.Text = _dRModel.ECGParamsData1.STPotential.ToString();
            textBox_血氧值.Text = _dRModel.SPO2Data1.SPO2Value.ToString();
            textBox_脉率值.Text = _dRModel.SPO2Data1.PulseRate.ToString();
            textBox_呼吸率.Text = _dRModel.ECGParamsData1.BreathRate.ToString();

            textBox_平均压.Text = _dRModel.NIBPData1.AveragePressure.ToString();
            textBox_收缩压.Text = _dRModel.NIBPData1.SystolicPressure.ToString();
            textBox_舒张压.Text = _dRModel.NIBPData1.DiastolicPressure.ToString();
            textBox_袖带压.Text = _dRModel.NIBPData1.CuffPressure.ToString();

            textBox_体温1.Text = _dRModel.TemperatureData1.TemperateValue1.ToString();
            textBox_体温2.Text = _dRModel.TemperatureData1.TemperateValue2.ToString();

            textBox_导联.Text = _dRModel.ECGParamsData1.ECGLeadState;
            textBox_心电状态.Text = _dRModel.ECGParamsData1.ECGLeadState;
            textBox_波形增益.Text = _dRModel.ECGParamsData1.ECGGain;
            textBox_滤波模式.Text = _dRModel.ECGParamsData1.ECGFilteringMode;

            textBox_血压测量模式.Text = _dRModel.NIBPData1.NIBPTestMode;
            textBox_血压测量状态.Text = _dRModel.NIBPData1.NIBPTestState;
            textBox_血压测量类型.Text = _dRModel.NIBPData1.NIBPTestType;

            textBox_血氧信号强度.Text = _dRModel.SPO2Data1.SignalQuality;
            textBox_血氧探头状态.Text = _dRModel.SPO2Data1.SPO2State;
            textBox_血氧值.Text = _dRModel.SPO2Data1.SPO2Value.ToString();
            zyGraphicWave4.DrawPoint(_dRModel.SPO2Data1.SPO2Value);//SPO2
        }

        private void toolStripButton_COM开_Click(object sender, EventArgs e)
        {
            bCtrV2.OpenPort(UpdateReceivedData);
            SwitchToolStripButtonEnabled(toolStripButton_COM开, toolStripButton_COM关);
        }

        private void toolStripButton_COM关_Click(object sender, EventArgs e)
        {
            bCtrV2.ClosePort();
            label_ETime.Text = DateTime.Now.ToString("G");
            SwitchToolStripButtonEnabled(toolStripButton_COM关, toolStripButton_COM开);
        }

        private void toolStripButton_NIBP开始_Click(object sender, EventArgs e)
        {
            bCtrV2.NIBPTestStart();
            SwitchToolStripButtonEnabled(toolStripButton_NIBP开始, toolStripButton_NIBP结束);
        }

        private void toolStripButton_NIBP结束_Click(object sender, EventArgs e)
        {
            bCtrV2.NIBPTestStop();
            SwitchToolStripButtonEnabled(toolStripButton_NIBP结束, toolStripButton_NIBP开始);
        }

        /// <summary>
        /// 禁用第一个启用第二个
        /// </summary>
        void SwitchToolStripButtonEnabled(ToolStripButton tsb1, ToolStripButton tsb2)
        {
            tsb1.Enabled = false;
            tsb2.Enabled = true;
        }
    }
}
